﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETDBTableAdapters;

public partial class Administration_content_add : System.Web.UI.Page
{
    private ContentTableAdapter contentAdapter = new ContentTableAdapter();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            String idParam = Request.QueryString["id"];
            if (idParam != null)
            {
                int id = int.Parse(idParam);
                ASPNETDB.ContentDataTable items = contentAdapter.GetContentById(id);
                if (items.Rows.Count != 0)
                {
                    ASPNETDB.ContentRow item = (ASPNETDB.ContentRow)items.Rows[0];
                    ContentFormId.Value = id.ToString();
                    ContentFormTitle.Text = item["contentTitle"].ToString().Trim();
                    ContentFormText.Text = item["contentText"].ToString();
                    ContentFormDate.Value = item["contentDate"].ToString();

                    InsertButton.Visible = false;
                    UpdateButton.Visible = true;
                }
                else
                {
                    Session.Add("ErrorMessage", "Item not found");
                    Response.Redirect("/CMSProject/Administration/error.aspx", true);
                }            
            }
            else
            {
                String navIdParam = Request.QueryString["navID"];
                if (navIdParam != null)
                    ContentFormNavigation.SelectedValue = navIdParam;

                String date = DateTime.Now.ToString();
                ContentFormDate.Value = date;
                InsertButton.Visible = true;
                UpdateButton.Visible = false;
            }
        }        
    }
    protected void Insert(object sender, EventArgs e)
    {
        int navId = int.Parse(ContentFormNavigation.SelectedValue);
        String title = ContentFormTitle.Text.ToString();
        String text = ContentFormText.Text.ToString();
        String date = ContentFormDate.Value.ToString();

        contentAdapter.InsertContentItem(title, text, date, navId);
        Response.Redirect("index.aspx", true);
    }

    protected void Update(object sender, EventArgs e)
    {
        int id = int.Parse(ContentFormId.Value);
        int navId = int.Parse(ContentFormNavigation.SelectedValue);
        String title = ContentFormTitle.Text.ToString();
        String text = ContentFormText.Text.ToString();
        String date = ContentFormDate.Value.ToString();

        contentAdapter.UpdateContentItem(title,text,date,navId,id);
        Response.Redirect("index.aspx", true);
    }
}